<template>
  <a-spin :spinning="confirmLoading">
    <ants-form-container :disabled="formDisabled">
      <a-form slot="detail" :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="昵称" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['nickname']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="邮箱" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['email']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="网站地址" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['url']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="头像" :label-col="labelCol" :wrapper-col="wrapperCol">
              <ants-image-upload v-decorator="['avatar']"/>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="IP" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['ip']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="IP所在地" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['ipAddr']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="浏览器" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['browser']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="操作系统名称" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input v-decorator="['osName']" placeholder="..."/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="留言内容" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-textarea v-decorator="['content']" placeholder="..." :rows="5"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </ants-form-container>
  </a-spin>
</template>

<script>
import AntsFormContainer from '@/components/ants/AntsFormContainer'
import pick from 'lodash.pick'
import {httpAction} from '@/api/manage'
import AntsImageUpload from "@/components/ants/AntsImageUpload";

export default {
  name: 'TechnologyStackForm',
  components: {
    AntsImageUpload,
    AntsFormContainer
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 4}
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 20}
      },
      url: {
        add: '/leavingMessage/add',
        edit: '/leavingMessage/edit'
      },
      confirmLoading: false
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    }
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'nickname', 'email', 'url', 'avatar', 'ip', 'ipAddr', 'browser', 'osName', 'content'))
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          const formData = Object.assign(this.model, values)
          var url = ''
          var method = ''
          if (!this.model.id) {
            url = that.url.add
            method = 'post'
          } else {
            url = that.url.edit
            method = 'put'
          }
          httpAction(url, method, formData).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
